package com.cn.web.controller.admin;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.cn.bean.pojo.bc.Role;
import com.cn.bean.pojo.bc.User;
import com.cn.bean.vo.bc.RoleUserName;
import com.cn.config.config.Msg;
import com.cn.service.server.admin.AdminRoleServer;
import com.cn.service.server.admin.AdminUserServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@CrossOrigin
@RestController
public class AdminUserController {
    private final AdminUserServer adminUserServer;
    private AdminRoleServer adminRoleServer;

    @Autowired
    public AdminUserController(AdminUserServer adminUserServer, AdminRoleServer adminRoleServer) {
        this.adminUserServer = adminUserServer;
        this.adminRoleServer = adminRoleServer;
    }

    @RequestMapping("/select/Admin/Role/IsRole")
    @ResponseBody
    public RoleUserName selectAdminRoleIsRole(@RequestBody JSONObject jsonObject) {
        RoleUserName roleUserName = new RoleUserName();
        String username = jsonObject.getString("username");
        List<Role> roles = adminRoleServer.selectRole();
        List<Role> roles1 = adminRoleServer.selectRoleUserName(username);
        roleUserName.setRoleListAll(roles);
        roleUserName.setRoleListUserName(roles1);
        return roleUserName;
    }

    @RequestMapping("/select/Role/UserName")
    @ResponseBody
    public List<Role> selectRoleUserName(@RequestBody JSONObject jsonObject) {
        String username = jsonObject.getString("username");
        List<Role> roles = adminRoleServer.selectRole();
        return roles;
    }

    @RequestMapping("/select/Admin/User")
    @ResponseBody
    public List<User> selectAdminUser(@RequestBody JSONObject jsonObject) {
        Integer page = jsonObject.getInteger("page");
        Integer limit = jsonObject.getInteger("limit");
        String username = jsonObject.getString("username");
        String name = jsonObject.getString("name");
        username = (username != null) ? username : "";
        name = (name != null) ? name : "";
        List<User> users = adminUserServer.selectAdminUser(username, name, limit, page);
        return users;
    }

    @RequestMapping("/Insert/Admin/UserRole")
    @ResponseBody
    public Msg InsertAdminUserRole(@RequestBody JSONObject jsonObject) {
        Msg msg = new Msg();
        int userId = jsonObject.getInteger("userId");
        JSONArray roleIdArray = jsonObject.getJSONArray("roleIdArray");
        int i1 = adminUserServer.deleteAdminUserRole(userId);
        for (int i = 0; i < roleIdArray.size(); i++) {
            int roleId = roleIdArray.getInteger(i);
            int i2 = adminUserServer.InsertAdminUserRole(userId, roleId);
        }
        if (i1>0){
            msg.setCode(200);
            msg.setMessage("保存成功");
            return msg;
        }
        msg.setCode(500);
        msg.setMessage("保存失败");
        return msg;
    }

    @RequestMapping("/insert/Admin/User")
    @ResponseBody
    public Msg insertAdminUser(@RequestBody JSONObject jsonObject) {
        Msg msg = new Msg();
        String username = jsonObject.getString("username");
        String password = jsonObject.getString("password");
        Integer status = jsonObject.getInteger("status");
        String name = jsonObject.getString("name");
        String bmmc = jsonObject.getString("bmmc");
        String sszz = jsonObject.getString("sszz");
        Integer departmentId = jsonObject.getInteger("departmentId");
        int usernameCount = adminUserServer.repeatUsername(username);
        if (usernameCount > 0) {
            msg.setCode(500);
            msg.setMessage("员工工号已存在，请重新输入");
            return msg;
        }
        int userId = adminUserServer.getAutoId() + 1;
        LocalDateTime now = LocalDateTime.now();
        // 定义日期时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 格式化日期时间
        String register_time = now.format(formatter);
        int i = adminUserServer.insertAdminUser(username, password, status, name, bmmc, sszz, departmentId, userId, register_time);
        if (i > 0) {
            msg.setCode(200);
            msg.setMessage("保存成功");
            return msg;
        }
        msg.setCode(500);
        msg.setMessage("保存失败");
        return msg;
    }

    @RequestMapping("/update/Admin/User")
    @ResponseBody
    public Msg updateAdminUser(@RequestBody JSONObject jsonObject) {
        Msg msg = new Msg();
        String username = jsonObject.getString("username");
        String password = jsonObject.getString("password");
        Integer status = jsonObject.getInteger("status");
        String name = jsonObject.getString("name");
        String bmmc = jsonObject.getString("bmmc");
        String sszz = jsonObject.getString("sszz");
        Integer departmentId = jsonObject.getInteger("departmentId");
        Integer userId = jsonObject.getInteger("userId");
        LocalDateTime now = LocalDateTime.now();
        // 定义日期时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 格式化日期时间
        String update_time = now.format(formatter);
        List<User> users = adminUserServer.selectUserId(userId);
        String username1 = users.get(0).getUsername();
        if (!username1.equals(username)) {
            int usernameCount = adminUserServer.repeatUsername(username);
            if (usernameCount > 0) {
                msg.setCode(500);
                msg.setMessage("员工工号已存在，请重新输入");
                return msg;
            }
        }
        int i = adminUserServer.updateAdminUser(username, password, status, name, bmmc, sszz, departmentId, userId, update_time);
        if (i > 0) {
            msg.setCode(200);
            msg.setMessage("修改成功");
            return msg;
        }
        msg.setCode(500);
        msg.setMessage("修改失败");
        return msg;
    }

    @RequestMapping("/delete/Admin/User")
    @ResponseBody
    public Msg deleteAdminUser(@RequestBody JSONObject jsonObject) {
        Integer userId = jsonObject.getInteger("userId");
        Msg msg = new Msg();
        int i = adminUserServer.deleteAdminUser(userId);
        if (i > 0) {
            msg.setCode(200);
            msg.setMessage("删除成功");
            return msg;
        }
        msg.setCode(500);
        msg.setMessage("删除失败");
        return msg;
    }
    @RequestMapping("/update/Admin/upUserPassWord")
    @ResponseBody
    public Msg updateAdminupUserPassWord(@RequestBody JSONObject jsonObject) {
        Msg msg = new Msg();
        String userName = jsonObject.getString("userName");
        String userPassWord = jsonObject.getString("userPassWord");
        int i = adminUserServer.upUserPassWord(userName, userPassWord);
        if (i > 0) {
            msg.setCode(200);
            msg.setMessage("修改成功");
            return msg;
        }
        msg.setCode(500);
        msg.setMessage("修改失败");
        return msg;
    }
}
